package com.lin.missyou.service;

import com.lin.missyou.core.enumeration.CouponStatus;
import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.exception.http.ParameterException;
import com.lin.missyou.model.Activity;
import com.lin.missyou.model.Coupon;
import com.lin.missyou.model.UserCoupon;
import com.lin.missyou.repository.ActivityRepository;
import com.lin.missyou.repository.CouponRepository;
import com.lin.missyou.repository.UserCouponRepository;
import com.lin.missyou.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author gcq
 * @Create 2021-11-03
 */
@Service
public class CouponService {
    @Autowired
    CouponRepository couponRepository;

    @Autowired
    ActivityRepository activityRepository;

    @Autowired
    UserCouponRepository userCouponRepository;


    public List<Coupon> getByCategory(Long cid)
    {
        Date now = new Date();
        return couponRepository.findByCategory(cid, now);
    }

    public List<Coupon> getWholeStoreCoupons() {
        Date now = new Date();
        return couponRepository.getWholeStoreCoupons(true, now);
    }

    /**
     * 编程方式
     *  用 Optional 做判断 不用写很多的else
     *
     *  orElseThrow
     * @param uid
     * @param couponId
     */
    public void collectOneCoupon(Long uid, Long couponId) {
        // 查询是否有对应的优惠券
        this.couponRepository.findById(couponId).
                orElseThrow(() -> new NotFoundException(40003));
        // 查询到优惠券对应的活动
        Activity activity = this.activityRepository.
                findByCouponListId(couponId)
                .orElseThrow(() -> new NotFoundException(40010));
        // 优惠券是否过期
        Date now = new Date();
        Boolean isIn = CommonUtil.isInTimeLine(now, activity.getStartTime(), activity.getEndTime());
        if(!isIn)
        {
            throw new ParameterException(40006);
        }
        // 优惠券是否还能领取
        Optional<UserCoupon> firstByUserIdAndCouponId = this.userCouponRepository.
                findFirstByUserIdAndCouponId(uid, couponId);
        if(firstByUserIdAndCouponId.isPresent()){
            throw new ParameterException(40005);
        }

        UserCoupon userCoupon = UserCoupon.builder()
                .userId(uid)
                .couponId(couponId)
                .status(CouponStatus.AVAILABLE.getValue())
                .createTime(now)
                .build();
        this.userCouponRepository.save(userCoupon);
    }

    /**
     * 获取当前用户可用的优惠卷
     * @param uid
     * @return
     */
    public List<Coupon> getMyAvailableCoupons(Long uid)
    {
        Date now = new Date();
        return couponRepository.findMyAvailable(uid, now);
    }

    /**
     * 获取已经使用过的优惠券
     * @param uid
     * @return
     */
    public List<Coupon> getMyUsedCoupons(Long uid)
    {
        Date now = new Date();
        return couponRepository.findMyUsed(uid, now);
    }

    /**
     * 获取已过期的优惠券
     * @param uid
     * @return
     */
    public List<Coupon> getMyExpiredCoupons(Long uid)
    {
        Date now = new Date();
        return couponRepository.findMyExpired(uid, now);
    }
}